<template>
    <div class="title-editor">
        <!-- 左侧：组件列表 -->
        <div class="panel left">
            <h3>基础组件</h3>
            <div class="comp-list">
                <div
                    v-for="(comp, i) in comps"
                    :key="i"
                    class="comp-item"
                    @click="addBlock(i)"
                    :class="{ disabled: comp.added >= comp.max }"
                >
                    <div class="comp-img">
                        <img :src="comp.img" alt="预览" class="preview-img" />
                    </div>
                    <div class="comp-info">
                        <div class="comp-name">{{ comp.name }}</div>
                        <div class="comp-count">{{ comp.added }}/{{ comp.max }}</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 中间：预览区 -->
        <div class="panel middle">
            <h3>预览区域</h3>
            <div
                v-for="(block, i) in blocks"
                :key="block.id"
                class="preview-item"
                :class="{ active: activeIdx === i }"
                @click="selectBlock(i)"
                :style="{
                    backgroundColor: block.bgColor,
                    alignItems: block.align === 'left' ? 'flex-start' : 'center',
                    paddingLeft: block.align === 'left' ? '20px' : '0',
                }"
            >
                <h2
                    :style="{
                        fontSize: block.titleSize + 'px',
                        fontWeight: block.titleWeight,
                        color: block.titleColor,
                        textAlign: block.align,
                    }"
                >
                    {{ block.title || `标题 ${block.type + 1}-${block.num}` }}
                </h2>
                <p
                    :style="{
                        fontSize: block.subSize + 'px',
                        color: block.subColor,
                        textAlign: block.align,
                    }"
                >
                    {{ block.subTitle || `副标题 ${block.type + 1}-${block.num}` }}
                </p>
            </div>
        </div>

        <!-- 右侧：配置区 -->
        <div class="panel right">
            <TitleConfig v-if="activeIdx !== -1" :config="curConfig" @save="saveConfig" />
            <div v-else class="empty-tip">请选择标题块配置</div>
        </div>
    </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import TitleConfig from './TitleConfig.vue'

// 生成唯一ID
const genId = () => Date.now() + Math.floor(Math.random() * 1000)

// 组件列表数据
const comps = ref([
    {
        name: '主标题块',
        img: 'https://pic.rmb.bdstatic.com/bjh/down/3e226dbb4175277c84f21cc3288b7fb2.gif',
        added: 0,
        max: 5,
        type: 0,
    },
    {
        name: '副标题块',
        img: 'https://pic.rmb.bdstatic.com/bjh/down/3e226dbb4175277c84f21cc3288b7fb2.gif',
        added: 0,
        max: 5,
        type: 1,
    },
    {
        name: '标题组合',
        img: 'https://pic.rmb.bdstatic.com/bjh/down/3e226dbb4175277c84f21cc3288b7fb2.gif',
        added: 0,
        max: 5,
        type: 2,
    },
    {
        name: '带图标题',
        img: 'https://pic.rmb.bdstatic.com/bjh/down/3e226dbb4175277c84f21cc3288b7fb2.gif',
        added: 0,
        max: 5,
        type: 3,
    },
    {
        name: '简约标题',
        img: 'https://pic.rmb.bdstatic.com/bjh/down/3e226dbb4175277c84f21cc3288b7fb2.gif',
        added: 0,
        max: 5,
        type: 4,
    },
])

// 标题块数据
const blocks = ref([])
const activeIdx = ref(-1)
const curConfig = ref({})

// 添加标题块
const addBlock = (i) => {
    const comp = comps.value[i]
    if (comp.added < comp.max) {
        comp.added++
        blocks.value.push({
            id: genId(),
            type: comp.type,
            num: comp.added,
            title: `${comp.name} ${comp.added}`,
            subTitle: `${comp.name} 副标题 ${comp.added}`,
            titleSize: 16,
            subSize: 12,
            titleWeight: 'bold',
            titleColor: '#000',
            subColor: '#666',
            bgColor: '#fff',
            align: 'center',
        })
        activeIdx.value = blocks.value.length - 1
        curConfig.value = { ...blocks.value[activeIdx.value] }
    }
}

// 选择标题块
const selectBlock = (i) => {
    activeIdx.value = i
    curConfig.value = { ...blocks.value[i] }
}

// 保存配置
const saveConfig = (newConfig) => {
    if (activeIdx.value !== -1) {
        blocks.value[activeIdx.value] = {
            ...blocks.value[activeIdx.value],
            ...newConfig,
        }
    }
}

// 监听选中块变化
watch(
    () => blocks.value[activeIdx.value],
    (newVal) => {
        if (newVal) curConfig.value = { ...newVal }
    },
    { deep: true }
)
</script>

<style scoped>
.title-editor {
    display: flex;
    height: 100vh;
    gap: 10px;
    padding: 10px;
    box-sizing: border-box;
    margin: 0;
    overflow: hidden;
}

.panel {
    flex: 1;
    border: 1px solid #ccc;
    padding: 15px;
    box-sizing: border-box;
    overflow-y: auto;
}

.panel h3 {
    margin-top: 0;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
    text-align: center;
}

/* 组件列表样式 */
.comp-list {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    padding: 10px 0;
}

.comp-item {
    flex: 0 0 calc(50% - 8px);
    max-width: calc(50% - 8px);
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 15px;
    box-sizing: border-box;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: #fff;
}

.comp-img {
    width: 100px;
    height: 100px;
    background: #f0f0f0;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 12px;
    overflow: hidden;
}

.preview-img {
    width: 80px;
    height: 80px;
    object-fit: contain;
}

.comp-info {
    text-align: center;
    width: 100%;
}

.comp-name {
    font-size: 15px;
    color: #333;
    margin-bottom: 6px;
    font-weight: 500;
}

.comp-count {
    font-size: 13px;
    color: #666;
    background: #f5f5f5;
    display: inline-block;
    padding: 3px 10px;
    border-radius: 15px;
}

.comp-item:hover:not(.disabled) {
    border-color: #42b983;
    background: #f5fbf8;
    transform: translateY(-2px);
}

.comp-item.disabled {
    opacity: 0.6;
    cursor: not-allowed;
    background: #f5f5f5;
}

/* 预览区样式 */
.preview-item {
    padding: 30px 20px;
    margin-bottom: 15px;
    border: 1px solid #eee;
    cursor: pointer;
    transition: all 0.2s;
    border-radius: 4px;
    min-height: 120px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.preview-item.active {
    border-color: #ff7b00;
    box-shadow: 0 0 0 2px rgba(66, 185, 131, 0.2);
}

.preview-item h2,
.preview-item p {
    width: 100%;
    margin: 0 0 8px 0;
}

.preview-item p {
    margin: 0;
    opacity: 0.8;
}

/* 配置区样式 */
.empty-tip {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #999;
    border: 1px dashed #ccc;
    border-radius: 4px;
    min-height: 300px;
}

/* 响应式调整 */
@media (max-width: 768px) {
    .comp-item {
        flex: 0 0 calc(100% - 8px);
        max-width: 100%;
    }
}
</style>